<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Home_Home extends Controller_Portal {

    public function before() {
        parent::before();
        $this->add_css(array('style', 'customSelectBox'));
        $this->add_js(array('ajax', 'SelectBox_1'));
    }

    public function action_thele() {
       /* $s_facebook = new Library_Facebook_Base(array(
            'appId' => '460937760716160',
            'secret' => '12d8993b3ce7a68621849f2d4b516fdc',
        ));
        $userfb = $s_facebook->getUser();
        $urlapp = 'https://www.facebook.com/Toshiba.com.vn/app_460937760716160?ref=page_internal';
        if (empty($userfb)) {
            $url = $s_facebook->getLoginUrl(array('scope' => 'user_about_me%2Cemail%2Cuser_status%2Cpublish_actions%2Cuser_likes'));
            $url = Controller_Helper_Url::url_query(array('redirect_uri' => urlencode($urlapp)), $url);
            echo "<script>window.parent.location = '$url';</script>";
            die;
        }
        try {
            $likes = $s_facebook->api("/me/likes/564078413625390");

            if (empty($likes['data'])) {
                $this->template->content = View::factory('home/like');
                
            }else{
                $this->template->content = View::factory('home/thele');
            }
        } catch (Exception $ex) {
            
        }*/
        $this->template->content = View::factory('home/thele');
        
    }

    public function action_get() {
        header('Content-type: application/json');
        $result = array('error' => 0, 'v1' => '_ _', 'v2' => "_ _", "v3" => '_ _', 'fullname' => '', 'total' => '_ _');
        $cmnd = trim(Arr::get($_POST, 'cmnd', ''));
        if ($cmnd == "") {
            $result['error'] = 1;
            echo json_encode($result);
            die;
        }

        $s_customer = new Model_Service_Customer();
        $en_customer = $s_customer->get($cmnd)->get_data();
        if ($en_customer->getId() <= 0) {
            $result['error'] = 1;
            echo json_encode($result);
            die;
        }

        $s_customerpoint = new Model_Service_CustomerPoint();
        $s_event = new Model_Service_Event();
        $a_event = $s_event->load_all(false, 0, 1, null, array('id' => 'desc'));
        $en_event = $a_event[0]->get_data();
        $en_customerpoint = $s_customerpoint->get($en_customer->getId(), $en_event->getId())->get_data();
        $result['fullname'] = $en_customer->getfullname();
        $result['v1'] = (int) $en_customerpoint->getweek1();
        $result['v2'] = (int) $en_customerpoint->getweek2();
        $result['v3'] = (int) $en_customerpoint->getweek3();
        $result['total'] = $result['v1'] + $result['v2'] + $result['v3'];
        echo json_encode($result);
        die;
    }

    public function action_like() {
       echo "ddd";die;
        
    }

}
